﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCommunity.Caching;

namespace XCommunity.Frames
{

  /// <summary>
  /// 话题框架的缓存容器
  /// </summary>
  public class TopicFrameCache : CacheContainer
  {


    public TopicFrameCache( FrameService service )
    {
      _service = service;
    }



    private FrameService _service;

    /// <summary>
    /// 获取框架服务
    /// </summary>
    protected FrameService FrameService
    {
      get { return _service; }
    }


    /// <summary>
    /// 添加一个话题框架到缓存
    /// </summary>
    /// <param name="topic"></param>
    public void AddItem( ITopicFrame topic )
    {
      AddItem( topic.TopicId, topic );
    }


    /// <summary>
    /// 从缓存中获取一个话题框架
    /// </summary>
    /// <param name="topicId"></param>
    /// <returns></returns>
    public ITopicFrame GetTopic( int topicId )
    {
      return (ITopicFrame) TryGetValue( topicId );
    }



    /// <summary>
    /// 获取或加载一个话题框架
    /// </summary>
    /// <param name="topicId"></param>
    /// <param name="loader"></param>
    /// <returns></returns>
    public ITopicFrame GetOrLoadTopic( int topicId, Func<int, ITopicFrame> loader )
    {
      return GetOrLoadValue( topicId, loader );
    }



    /// <summary>
    /// 从缓存中移除一个话题框架
    /// </summary>
    /// <param name="topicId"></param>
    public void RemoveTopic( int topicId )
    {
      RemoveCacheItem( topicId );
    }
  }
}
